#include "filesystem.h"
#include "directory.h"
#include "inode.h"
#include "blockFreeList.h"
#include "disk.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define NEWLINES_PLUS_SLASH 3
#define SUCCESS 1

file_t* openFile(char* filename){
  if(root == NULL){
	  root = malloc(sizeof(directoryNode_t*));
	  root->name = "/";
	  root->inodeNumber = 6;
  }
  char* currentDir = "/share/";
  int pathstrlen = strlen(filename) + strlen(currentDir) + NEWLINES_PLUS_SLASH;
  char* filepath = malloc(sizeof(char)*(pathstrlen));
  strcpy(filepath, currentDir);
  strcat(filepath, filename);
  file_t* file = malloc(sizeof(file_t));
  directoryNode_t* dir = getDirectory(filepath);

  //this means the file wont exist
  if(dir == NULL){
	inode_t* newFile = malloc(sizeof(inode_t));
	int inodeNumber = getUnusedInodeNumber();
	setBlockToUsed(inodeNumber);
	newFile->inodeNumber = inodeNumber;
	int i;
	for(i = 0; i < 11; i++){
		int blockNumber = getUnusedBlockNumber();
		newFile->inodeBlockList[i] = blockNumber;
		setBlockToUsed(blockNumber);
	}
	directoryNode_t* parent = getDirectory(currentDir);
	dir = malloc(sizeof(directoryNode_t));
	dir->inodeNumber = newFile->inodeNumber;
	dir->name = filename;
	addChild(parent, dir);
	int disknum = openDisk("root", DISK_SIZE);
	writeBlock(disknum, SUPER_BLOCK_NUMBER, freelist.bitmap);


  }

  file->inodeNumber = dir->inodeNumber;
  file->filepath = filepath;
  return file;
}

void closeFile(file_t* fp){
	free(fp->filepath);
	free(fp);
}

char* readFile(file_t* file){

  directoryNode_t* dir = getDirectory(file->filepath);
  inode_t* node = getInode(dir->inodeNumber);
  int disknum = openDisk("root", DISK_SIZE);
  int contents[11][1024/sizeof(int)];
  int i;
  for(i = 0; i < 11; i++){
	  readBlock(disknum, node->inodeBlockList[i], contents[i]);
  }
  int* notparsed = concat(contents);
  char* parsed = intstostring(notparsed);
  return parsed;

}

//this assumes the file already exists
int writeFile(file_t* file, char* contents){
	directoryNode_t* dir = getDirectory(file->filepath);
	inode_t* node = getInode(dir->inodeNumber);
	int* parsedContents = stringtoints(contents);
	//int** blocks = unconcat(parsedContents);
	int disknum = openDisk("root", DISK_SIZE);
	int i;
	for(i = 0; parsedContents[i*1024/sizeof(int)] != 0; i++){

		int* tempArray = malloc(1024);
		writeBlock(disknum, node->inodeBlockList[i], memcpy(tempArray, parsedContents, 1024));
		free(tempArray);
	}
	return SUCCESS;
}

int* concat(int contents[11][1024/sizeof(int)]){
	int i;
	int* bigblock = malloc(11*1024/sizeof(int));
	for(i = 0; i< 11; i++){
		memcpy(bigblock+i*1024/sizeof(int), contents[i], 1024/sizeof(int));

	}

	return bigblock;

}
/*int** unconcat(int* parsedContents){
	int *blocks[1024];
	blocks[0] = parsedContents;
	blocks[1] = parsedContents+1024;
	return blocks;
}*/


char** getFileNameList(char* directoryName){
	directoryName = "/share";
	directoryNode_t* dir = getDirectory(directoryName);
	char** filenames = malloc(dir->numberOfChildren*sizeof(char*));
	directoryNode_t** children = dir->children;
	int i;
	for(i = 0; i < dir->numberOfChildren; i++){
		filenames[i] = children[i]->name;
	}
	return filenames;

}
int numberOfFiles(char* fullpath){
	directoryNode_t* dir = getDirectory(fullpath);
	return dir->numberOfChildren;
}



